<?php


namespace app\find;


class binaryFind
{
    public function find($arr, $value, $leftIndex, $rightIndex)
    {
        if ($leftIndex > $rightIndex) {
            return null;
        }

        $middleIndex = floor(($leftIndex + $rightIndex) / 2);
        if ($value < $arr[$middleIndex]) {
            $index = $this->find($arr, $value, 0, $middleIndex - 1);
        } elseif ($value > $arr[$middleIndex]) {
            $index = $this->find($arr, $value, $middleIndex + 1, $rightIndex);
        } else {
            $index = $middleIndex;
        }

        return $index;
    }
}